import Vue from 'vue'
import Router from 'vue-router'
// import home from '@/views/home'
// import detail from '@/views/detail'
// import read from '@/views/read'
// import notPage from '@/views/notPage'
// import bookCity from '@/views/bookCity'
// import bookShelf from '@/views/bookShelf'
// import login from '@/views/login'

Vue.use(Router)

let router =  new Router({
    linkActiveClass:'active',
    // mode: 'history',
  routes: [
    {
        path:'/',
        redirect:'/home/bookCity'
    },
    {
        path: '/home',
        component: () => import('@/views/home'),
        children:[
            {
                path:'/home/bookCity',
                meta:{title:'书城'},
                component:() => import('@/views/bookCity')
            },
            {
                path:'/home/bookShelf',
                meta:{title:'书架'},
                component:() => import('@/views/bookShelf')
            }
        ]
    },
    {
        path: '/detail/:fiction_id',
        name:'detail',
        meta:{title:'详情'},
        component: () => import('@/views/detail')
    },
    {
        path: '/read/:fiction_id/:chapter_id',
        name:'read',
        meta:{title:'阅读'},
        component: () => import('@/views/read'),
        // beforeEnter(to,from,next){
        //     let uid = window.localStorage.getItem('uid') || '';
        //     if(uid){
        //         next();
        //     }else{
        //         next('/login')
        //     }
        // }
    },
    {
        path:'/login',
        alias:'/test',  //别名
        meta:{title:'登录'}, //路由元信息
        component:() => import('@/views/login')
    },
    {
        path:'*',
        meta:{title:'404'},
        component:() => import('@/views/notPage')
    }
  ]
})

let arr = ['read','detail']
//全局前置守卫
router.beforeEach((to,from,next) => {
    console.log(arr.includes(to.name))
    if(arr.includes(to.name)){
        let uid = window.localStorage.getItem('uid') || '';
        if(uid){
            next();
        }else{
            next('/login')
        }
    }else{
        next()
    }
})

//全局后置钩子
router.afterEach((to, from) => {
    console.log(to);
    // ...
    if(to.meta && to.meta.title){
        document.title = to.meta.title;
    }else{
        document.title = '书城'
    }
})

export default router